package com.study.spmongodb.java8;


import java.util.function.LongConsumer;
import java.util.stream.LongStream;

/**
 * @author MI
 * @version 1.0
 * @date 2020/10/20 11:07
 */
// 并行流|https://mrbird.cc/Java8-parallelStream.html
public class StreamExample {

    public static void main(String[] args) {
        Integer in = -1;
        System.out.println(in);

        //在处理数据集规模不大的情况下，将流并行化所带来的额外开销比逻辑代码开销还大
        StreamExample.test((n) -> LongStream.rangeClosed(1L, n).reduce(0L, Long::sum), 100L);
        StreamExample.test((n) -> LongStream.rangeClosed(1L, n).parallel().reduce(0L, Long::sum), 100L);
    }

    static void test(LongConsumer c, Long n) {
        final long start = System.currentTimeMillis();
        c.accept(n);
        final long end = System.currentTimeMillis();
        System.out.println("处理时间：" + (end - start) + "msc");

    }
}
